package com.hc.webservice;

import java.io.File;
import java.io.IOException;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.HttpVersion;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.mime.MultipartEntity;
import org.apache.http.entity.mime.content.ContentBody;
import org.apache.http.entity.mime.content.FileBody;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.params.CoreProtocolPNames;

public class SendImageHttpPost {
	 public static void sendPost(String url, String imagePath, String toketype, String accessToken) throws IOException, ClientProtocolException  {
         HttpClient httpclient = new DefaultHttpClient();
         httpclient.getParams().setParameter(CoreProtocolPNames.PROTOCOL_VERSION, HttpVersion.HTTP_1_1);

         HttpPost httppost = new HttpPost(url);
         httppost.setHeader("Content-Type",
					"application/x-www-form-urlencoded;charset=UTF-8");
         httppost.setHeader("Authorization", toketype + " "
					+ accessToken);
         File file = new File(imagePath);

         MultipartEntity mpEntity = new MultipartEntity();
         ContentBody cbFile = new FileBody(file, "image/jpeg");
         mpEntity.addPart("userfile", cbFile);

         httppost.setEntity(mpEntity);
//         Log.e("executing request " + httppost.getRequestLine());
         HttpResponse response = httpclient.execute(httppost);
         HttpEntity resEntity = response.getEntity();
//         Log.e(""+response.getStatusLine());
         if (resEntity != null) {
//                   Log.e(EntityUtils.toString(resEntity));
         }
         if (resEntity != null) {
                   resEntity.consumeContent();
         }
         httpclient.getConnectionManager().shutdown();
}
}
